package com.hubery.designpattern.design.decorator.impl;

import com.hubery.designpattern.design.decorator.AbstractFillUserInfoDecorator;
import com.hubery.designpattern.design.decorator.dto.UserInfoDTO;
import com.hubery.designpattern.enums.UserTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author lixueyang
 * @since 2023/2/28 9:10
 */
@Slf4j
@Component
public class AppFillUserInfoDecorator extends AbstractFillUserInfoDecorator {

    private static final Map<String, UserInfoDTO> USER_OTHER_INFO_MAP = new HashMap<>();

    static {
        USER_OTHER_INFO_MAP.put("1", UserInfoDTO.builder().userTypeEnum(UserTypeEnum.GENERAL).vipRank(0).build());
        USER_OTHER_INFO_MAP.put("2", UserInfoDTO.builder().userTypeEnum(UserTypeEnum.VIP).vipRank(3).build());
    }

    @Override
    public UserInfoDTO fillUserInfo(String userId) {

        UserInfoDTO userInfoDTO = super.fillUserInfo(userId);
        if (Objects.isNull(userInfoDTO)) {
            return null;
        }

        UserInfoDTO userOtherInfo = USER_OTHER_INFO_MAP.getOrDefault(userId, null);
        if (Objects.isNull(userOtherInfo)) {
            return userInfoDTO;
        }

        userInfoDTO.setUserTypeEnum(userOtherInfo.getUserTypeEnum());
        userInfoDTO.setVipRank(userOtherInfo.getVipRank());

        log.info("用户 【{}】 业务数据填充", userInfoDTO.getUsername());
        return userInfoDTO;
    }
}
